﻿#include "StartScene.h"
#include "JNIClass.h"
#include "Config.h"
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "platform/android/jni/JniHelper.h"
#include "jni.h"
#endif
USING_NS_CC;
CCScene* StartScene::scene()
{
	// 'scene' is an autorelease object
	CCScene *scene = CCScene::create();

	// 'layer' is an autorelease object
	StartScene *layer = StartScene::create();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool StartScene::init()
{
	//////////////////////////////
	// 1. super init first
	if (!CCLayer::init())
	{
		return false;
	}

	visibleSize = CCDirector::sharedDirector()->getVisibleSize();

	// add "HelloWorld" splash screen"
	bgSprite = CCSprite::create("bg_start.png");
	bgSprite->setPosition(ccp(0, 0));
	bgSprite->setAnchorPoint(ccp(0, 0));
	bgSprite->setScaleX(visibleSize.width / bgSprite->getContentSize().width);
	bgSprite->setScaleY(visibleSize.height / bgSprite->getContentSize().height);
	addChild(bgSprite, 0);

	pMenu = CCMenu::create();
	pMenu->setPosition(ccp(0, 0));
	pMenu->setAnchorPoint(ccp(0.5, 0.5));
	this->addChild(pMenu, 1);

	CCMenuItemImage *levelButton = CCMenuItemImage::create(
		"Images/play.png",
		"Images/playhover.png",
		this, menu_selector(StartScene::menuCloseCallback));
	levelButton->setAnchorPoint(ccp(0.5, 0.5));
	levelButton->setPosition(ccp(visibleSize.width / 2, visibleSize.height * 0.55));
	/*levelButton->setScaleX(visibleSize.width / levelButton->getContentSize().width);
	levelButton->setScaleY(visibleSize.height / levelButton->getContentSize().height);*/
	float ratio = visibleSize.width / levelButton->getContentSize().width > visibleSize.height / levelButton->getContentSize().height ? visibleSize.height / levelButton->getContentSize().height : visibleSize.width / levelButton->getContentSize().width;
	levelButton->setScale(0.4);
	pMenu->addChild(levelButton);

	btn_ShareApp = CCMenuItemImage::create("Images/rate.png", "Images/ratehover.png", this, menu_selector(StartScene::btnRateApp));
	btn_ShareApp->setPosition(ccp(visibleSize.width / 2, visibleSize.height * 0.425));
	btn_ShareApp->setScale(0.4);
	pMenu->addChild(btn_ShareApp, 2);

	// add btn ABout Us
	btn_AboutUs = CCMenuItemImage::create("Images/about.png", "Images/abouthover.png", this, menu_selector(StartScene::btnAboutUs));
	btn_AboutUs->setPosition(ccp(visibleSize.width / 2, visibleSize.height * 0.3));
	/*btn_AboutUs->setScaleX(btn_AboutUs->getContentSize().width / visibleSize.width );
	btn_AboutUs->setScaleY(btn_AboutUs->getContentSize().height / visibleSize.height);*/
	btn_AboutUs->setScale(0.4);
	pMenu->addChild(btn_AboutUs, 2);
	
	this->setTouchEnabled(true);
	//// back press
	this->setKeypadEnabled(true);
	
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(kMusicEnd);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(kMusicBroken);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(kMusicHit);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(kMusicFirst);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(kMusicStart);
	
	// nap cache
	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(kPListCockRoach);
	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(kPListCockRoachDie);
	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("chuyendonggian/ChuyenDongGian0.plist");
	
	return true;
}

StartScene::~StartScene()
{
}

void StartScene::menuCloseCallback(CCObject* pSender)
{
	if(bgSprite!= NULL){
		bgSprite = NULL;
	}
	if(btn_AboutUs != NULL){
		btn_AboutUs = NULL;
	}

	if(btn_ShareApp!= NULL){
		btn_ShareApp = NULL;
	}

	if(pMenu!= NULL){
		pMenu = NULL;
	}

	CCDirector::sharedDirector()->purgeCachedData();
	CCScene *pScene = HelloWorld::scene();
	CCDirector::sharedDirector()->replaceScene(pScene);
}

void StartScene::keyBackClicked()
{
	CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
}

void StartScene::btnAboutUs(CCObject* pSender){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JNIClass::aboutUs();
#endif
}

void StartScene::btnRateApp(CCObject* pSender){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
	JNIClass::rateApp();
#endif
}
